package com.solo.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.solo.common.core.base.service.impl.BasicServiceImpl;
import com.solo.common.core.utils.StringUtils;
import com.solo.system.api.consts.menu.Meta;
import com.solo.system.api.consts.menu.Router;
import com.solo.system.api.entity.SysMenu;
import com.solo.system.mapper.SysMenuMapper;
import com.solo.system.model.menu.resp.MenuRoutersResp;
import com.solo.system.service.SysMenuService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.solo.system.api.entity.table.SysMenuTableDef.SysMenuTable;


/**
 * 菜单 Service实现类
 * @author 十一
 * @since 2023-11-14 14:13
 * 人生若只如初见，何事秋风悲画扇
 **/
@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl extends BasicServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<MenuRoutersResp> routers() {
        return mapper.routers();
    }

    @Override
    public List<Tree<Long>> buildRouters(List<MenuRoutersResp> menus, String type, Long parentId) {
        List<TreeNode<Long>> collect = menus.stream()
//                .filter(menuTypePredicate(type))
                .map(getNodeFunction())
                .collect(Collectors.toList());

        Long parent = parentId == null ? 0 : parentId;
        return TreeUtil.build(collect, parent);
    }

    @NotNull
    private Function<MenuRoutersResp, TreeNode<Long>> getNodeFunction() {
        return menu -> {
            String title = StringUtils.blankToDefault(menu.getTranslate(), menu.getName());
            TreeNode<Long> node = new TreeNode<>();
            node.setId(menu.getMenuId());
            node.setParentId(menu.getParentId());
            node.setName(title);
            node.setWeight(menu.getSort());
            // 扩展属性
            Map<String, Object> extra = new HashMap<>();
            extra.put(Router.PATH, menu.getPath());
            extra.put(Router.REDIRECT, null);
            extra.put(Router.COMPONENT, null);

            Map<String, Object> meta = new HashMap<>();
            meta.put(Meta.TITLE, title);
            meta.put(Meta.ICON, menu.getIcon());
            meta.put(Meta.SHOW_LINK, menu.getVisible());
            meta.put(Meta.SHOW_PARENT, true);
            // TODO 角色未设置
            meta.put(Meta.ROLES, null);
            List<String> auths = queryChain()
                    .select(SysMenuTable.Permission).from(SysMenuTable)
                    .where(SysMenuTable.ParentId.eq(menu.getMenuId()))
                    // TODO 此处修改成枚举
                    .and(SysMenuTable.Type.eq("B")).listAs(String.class);
            meta.put(Meta.AUTHS, auths);
            meta.put(Meta.KEEP_ALIVE, menu.getKeepAlive());
            meta.put(Meta.FRAME_SRC, null);
            meta.put(Meta.FRAME_LOADING, null);
            meta.put(Meta.HIDDEN_TAG, null);
            meta.put(Meta.DYNAMIC_LEVEL, null);
            extra.put(Router.META, meta);
            node.setExtra(extra);
            return node;
        };
    }

}
